package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.PersonalRecommendationsDO;
import com.example.makerspace.domain.TaskFinishDO;
import com.example.makerspace.mapper.PersonalRecommendationsMapper;
import com.example.makerspace.vo.ProductRecommendationVO;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class PersonalRecommendationsDAO extends  BaseDAO<PersonalRecommendationsDO> {
    @Resource
    private PersonalRecommendationsMapper personalRecommendationsMapper;
    public final static String USERNAME = "username";

    public final static String PRODUCT = "product_id";

    @PostConstruct
    public void init(){
        this.mapper = personalRecommendationsMapper;
    }

    public List<PersonalRecommendationsDO> selectByUserName(String username) {
        QueryWrapper<PersonalRecommendationsDO> wrapper = new QueryWrapper<>();
        wrapper.eq(USERNAME,username);
        return mapper.selectList(wrapper);
    }

    public PersonalRecommendationsDO selectByProductId(String username, Integer id) {
        QueryWrapper<PersonalRecommendationsDO> wrapper = new QueryWrapper<>();
        wrapper.eq(PRODUCT,id).eq(USERNAME,username);
        return mapper.selectOne(wrapper);

    }

    public List<PersonalRecommendationsDO> selectByUserNameAndSort(String username) {
        QueryWrapper<PersonalRecommendationsDO> wrapper = new QueryWrapper<>();
        wrapper.eq(USERNAME,username).orderByDesc("weight").orderByDesc("create_time");
        return  mapper.selectList(wrapper);
    }

    public Page<ProductRecommendationVO> getPersonRecommendSort(Page<ProductRecommendationVO> page, String username) {
        return page.setRecords(personalRecommendationsMapper.getProductPersonRecommend(page,username));
    }

    public void deleteByProductId(int id) {
        QueryWrapper<PersonalRecommendationsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",id);
        mapper.delete(wrapper);
    }

    public void deleteByUsername(String username) {
        mapper.delete(new QueryWrapper<PersonalRecommendationsDO>().eq(USERNAME,username));
    }

    public void deleteByUsernameAndId(String username, int id) {
        mapper.delete(new QueryWrapper<PersonalRecommendationsDO>().eq(USERNAME,username).eq(PRODUCT,id));
    }
}
